<?php
/**
 * 游戏基础操作类
 * 
 * Created	: 2010-10-20
 * Modified : 2010-10-20
 * @author  : 陈立强
 */
class Game extends MCBase {
	
	public static $data = array();

	/**
	 * 游戏运行初始化
	 */
//	public static function init ()
//	{
//		global $DB;
//		Street::init();//初始化区域街道的数据
//	}
	
	/**
	 * 游戏载入初始化数据
	 */
	public static function load()
	{
		global $G;
		$data = array();
		
		// 年份,季节,日期
		$data['game_date'] = C('game');
		$data['version'] = C('version');
		$data['image_url'] = $G['image_url'];
		
		return array(
			'val'	=> true,
			'data'	=> $data
		);
	}
	
	/**
	 * 游戏载入初始配置
	 * @param string $type
	 */
//	public static function loadCfg($uid)
//	{
//		global $G;
//		$cfgList = array();
//		$version = C('version');
//		$cfgList += $version;
//		// 全局配置
//		$cfgList['image_url'] 		= $G['image_url'];
//		$cfgList['id_quality']		= $G['id_quality'];
//		$cfgList['id_color_name']	= $G['id_color_name'];
//		$cfgList['market_list_page_size'] = $G['market_list_page_size'];
//		// 物品类型
//		$cfgList['item_type_list']	= C('item/item_type_list');
//		// 消耗点券配置
//		C('making/staff');
//		$cfgList['giveUpCard'] = $G['get_pk_prize_nochioce_ticket'];
//		$cfgList['next_proficiency_need_lvl'] = $G['next_proficiency_need_lvl'];
//		// 交换券
//		C('build/market');
//		$cfgList['market_max_ex_coupon'] = $G['market_max_ex_coupon'];
//		
//		// 种子配置
//		$cfg = C('item/crop');
//		$cfgList['crop_seed'] = array();
//		foreach ($cfg as $id=>$item) {
//			$cfgList['crop_seed'][$id] = array(
//				'plant_need_time' => $item['plant_need_time'],
//			);
//		}
//		$cfg = C('item/fruit');
//		$cfgList['fruit_seed'] = array();
//		foreach ($cfg as $id=>$item) {
//			$cfgList['fruit_seed'][$id] = array(
//				'grow_need_time' => $item['grow_need_time'],
//				'ripe_need_time' => $item['ripe_need_time'],
//			);
//		}
//		$cfg = C('item/animal');
//		$cfgList['animal_seed'] = array();
//		foreach ($cfg as $id=>$item) {
//			$cfgList['animal_seed'][$id] = array(
//				'grow_need_time' => $item['grow_need_time'],
//				'ripe_need_time' => $item['ripe_need_time'],
//			);
//		}
//		$cfg = C('item/gather');
//		$cfgList['gather_seed'] = array();
//		foreach ($cfg as $id=>$item) {
//			$cfgList['gather_seed'][$id] = array(
//				'work_need_time' => $item['work_need_time'],
//			);
//		}
//		
//		
//		// 农田适应度,上限
//		$cfg = C('build/farm');
//		$cfgList['farm_max_fit_val']	= $G['farm_max_fit_val'];
//		$cfgList['farm_fit_lvl'] 		= $G['farm_fit_lvl'];
//		$cfgList['farm_lvlup']			= $cfg;
//		// 果林活力值,上限
//		$cfg = C('build/fruit_forest');
//		$cfgList['fruit_max_vogorous']	= $G['fruit_max_vogorous'];
//		$cfgList['fruit_vogorous_lvl'] 	= $G['fruit_vogorous_lvl'];
//		$cfgList['ftree_lvlup']			= $cfg;
//		// 养殖心情值/饱食度,上限
//		$cfg = C('build/raise');
//		$cfgList['raise_max_mood_val']	= $G['raise_max_mood_val'];
//		$cfgList['raise_max_food_val']	= $G['raise_max_food_val'];
//		$cfgList['raise_mood_lvl'] 		= $G['raise_mood_lvl'];
//		$cfgList['raise_lvlup']			= $cfg;
//		// 采集富集度,上限
//		$cfg = C('build/gather');
//		$cfgList['gather_max_enrichment']	= $G['gather_max_enrichment'];
//		$cfgList['gather_enrichment_lvl'] 	= $G['gather_enrichment_lvl'];
//		$cfgList['gather_lvlup']			= $cfg;
//		
//		// 店铺
//		$cfg = C('build/shop');
//		//店铺排队区空位扩建需要的金钱和人气值 
//		$cfgList['shop_queue_space']			= $G['shop_queue_space'];
//		$cfgList['shop_queue_space_coin']		= $G['shop_queue_space_coin'];
//		//店铺排队区空位升级需要的金钱和人气值 
//		$cfgList['shop_queue_space_lvl']		= $G['shop_queue_space_lvl'];
//		$cfgList['shop_queue_space_lvl_coin']	= $G['shop_queue_space_lvl_coin'];
//		//店铺销售区空位扩建需要的金钱和人气值 
//		$cfgList['shop_sale_space']				= $G['shop_sale_space'];
//		$cfgList['shop_sale_space_coin']		= $G['shop_sale_space_coin'];
//		//店铺销售区空位升级需要的金钱和人气值 
//		$cfgList['shop_sale_space_lvl']			= $G['shop_sale_space_lvl'];
//		$cfgList['shop_sale_space_lvl_coin']	= $G['shop_sale_space_lvl_coin'];
//		
//		// 员工PK记录/奖励保留的时间(秒)
//		$cfgList['pk_log_save_seconds']			= $G['pk_log_save_seconds'];
//		
//		// 工坊
//		$cfg = C('build/making');
//		//不同类型的子类型数据
//		$cfgList['cook']		= $G['cook'];
//		$cfgList['drink']		= $G['drink'];
//		$cfgList['furniture']	= $G['furniture'];
//		$cfgList['cook_technics']			= arr2dimension($G['cook_technics']);
//		$cfgList['cook_param']				= arr2dimension($G['cook_param']);
//		$cfgList['drink_technics']			= arr2dimension($G['drink_technics']);
//		$cfgList['drink_param']				= arr2dimension($G['drink_param']);
//		$cfgList['furniture_technics']		= arr2dimension($G['furniture_technics']);
//		$cfgList['furniture_param']			= arr2dimension($G['furniture_param']);
//		$cfgList['making_item_title_list']	= array();
//		foreach ($G['cook'] as $value) {
//			$cfgList['making_item_title_list'][$value['item_id']] = $value['title'];
//		}
//		foreach ($G['drink'] as $value) {
//			$cfgList['making_item_title_list'][$value['item_id']] = $value['title'];
//		}
//		foreach ($G['furniture'] as $value) {
//			$cfgList['making_item_title_list'][$value['item_id']] = $value['title'];
//		}
//		//升级数据
//		$cfgLvlUp = array();
//		foreach ($G['make_building_lvlup_coin'] as $lvl=>$v) {
//			$cfgLvlUp[$lvl] = array('need_coin' => $v);
//		}
//		foreach ($G['make_building_lvlup_exp'] as $lvl=>$v) {
//			$cfgLvlUp[$lvl]['need_exp'] = $v;
//		}
//		$cfgList['make_building_max_lvl'] = $G['make_building_max_lvl'];
//		$cfgList['makingHouse'] = array(
//			'lvlUp' => $cfgLvlUp,
//		);
//		// 制造类型
//		$cfgList['make_class_list']	= array();
//		foreach ($G['make_class_list'] as $sign=>$v) {
//			$cfgList['make_class_list'][$sign]['title'] = $v;
//		}
//		foreach ($G['make_class_type_list'] as $sign=>$v) {
//			$cfgList['make_class_list'][$sign]['id'] = $v;
//		}
//		// 配方配置
//		$cfgList['recipe'] = array();
//		foreach ($G['recipe'] as $product_lvl=>$v) {
//			$cfgList['recipe'][$product_lvl] = array();
//			foreach ($v as $lvl=>$vv) {
//				unset($vv['make_get_exp']);
//				$vv['quality'] *= 100;
//				$vv['lvlup_success'] *= 100;
//				$cfgList['recipe'][$product_lvl][$lvl] = $vv;
//			};
//		}
//		// 员工潜力值
//		C('making/staff');
//		$cfgList['state_val'] = $G['potential_lvl_list'];
//		$cfgList['train_add_val'] = $G['train_add_val'];
//		C('build/shop');
//		$cfgList['shop_service'] = array();
//		foreach ($G['shop_service_list'] as $key=>$value) {
//			$cfgList['shop_service'][$key] = $value['name'];
//		}
//		// 店铺扩建相关
//		C('build/shop');
//		$cfgList['shop_depot'] = $G['shop_depot'];
//		$cfgList['shop_depot_add_space_coin'] = $G['shop_depot_add_space_coin'];
//		//顾客最低品质配置
//		C('shop/guest');
//		$cfgList['guest_lvl_cfg'] = $G['guest_lvl_cfg'];
//		
//		return array(
//			'val'	=> true,
//			'data'	=> $cfgList
//		);
//	}
	
	/**
	 * 游戏载入初始配置
	 * @param string $type 类型:common,make,shop,staff,town
	 */
	public static function loadCfg($uid, $type='common')
	{
		global $G;
		$cfgList = array();
		$version = C('version');
		if (!$type) {
			$type = 'common';
		}
		switch ($type) {
			case 'common':
				$cfgList['image_url'] = $G['image_url'];
				$cfgList['id_quality']		= $G['id_quality'];
				$cfgList['id_color_name']	= $G['id_color_name'];
				$cfgList['next_proficiency_need_lvl'] = $G['pre_proficiency_lvl'];
				$cfgList['max_proficiency_lvl'] = $G['max_proficiency_lvl'];
				$cfgList['secmatTypeID'] = 51;
				$cfgList['item_type'] = array_flip(C('item/item_type'));
				$cfgList['id_color_name']	= $G['id_color_name'];
				// 仓库扩建需要的点券等配置
				$cfg = C('build/depot');
				$cfgList['add_capacity_per_times']	= $G['add_capacity_per_times'];
				$cfgList['add_depot_capacity_need_ticket']	= $G['add_depot_capacity_need_ticket'];
				// 员工一些信息
				C('making/staff');
				$cfgList['train_add_val']		= $G['train_add_val'];
				$cfgList['pk_log_save_seconds']	= $G['pk_log_save_seconds'];
				// 农田适应度,上限
				$cfg = C('build/farm');
				$cfgList['farm_lvlup']			= $cfg;
				$cfgList['farm_fit_lvl']		= $G['farm_fit_lvl'];
				$cfgList['farm_max_fit_val']	= $G['farm_max_fit_val'];
				// 果林活力值,上限
				$cfg = C('build/fruit_forest');
				$cfgList['fruit_max_vogorous']	= $G['fruit_max_vogorous'];
				$cfgList['fruit_vogorous_lvl'] 	= $G['fruit_vogorous_lvl'];
				$cfgList['ftree_lvlup']			= $cfg;
				// 养殖心情值/饱食度,上限
				$cfg = C('build/raise');
				$cfgList['raise_max_mood_val']	= $G['raise_max_mood_val'];
				$cfgList['raise_max_food_val']	= $G['raise_max_food_val'];
				$cfgList['raise_mood_lvl'] 		= $G['raise_mood_lvl'];
				$cfgList['raise_lvlup']			= $cfg;
				$cfgList['feeding_quality_add_mood_val'] = $G['feeding_quality_add_mood_val'];
				$cfgList['feeding_quality_add_food_val'] = $G['feeding_quality_add_food_val'];
				// 采集富集度,上限
				$cfg = C('build/gather');
				$cfgList['gather_max_enrichment']	= $G['gather_max_enrichment'];
				$cfgList['gather_enrichment_lvl'] 	= $G['gather_enrichment_lvl'];
				$cfgList['gather_lvlup']			= $cfg;
				$cfgList = array_merge($cfgList, C('common/need_ticket'));
				// 物品类型
				$cfgList['item_type_list']	= C('item/item_type_list');
				$cfgList['proficiency_exp']	= C('role/proficiency_exp');
				// 邮件
				C('build/mail');
				$cfgList['mail_attachment_expire'] = $G['mail_attachment_expire'];
				// 工坊(产品比赛)
				C('build/making');
				$cfgList['product_pk_time'] = $G['product_pk_time'];
				// 互动操作
				C('common/interactive');
				$cfgList['energy_default_value'] = $G['energy_default_value'];
				$cfgList['weed_get_exp'] = $G['weed_get_exp'];
				$cfgList['worm_get_exp'] = $G['worm_get_exp'];
				$cfgList['clean_get_exp'] = $G['clean_get_exp'];
				$cfgList['maintain_get_exp'] = $G['maintain_get_exp'];
				break;
				
			case 'make':
				$cfg = C('build/making');
				//建筑最大等级
				$cfgList['make_building_max_lvl'] = $G['make_building_max_lvl'];
				// 配方最高档次等级
				$cfgList['recipe_max_lvl'] = $G['recipe_max_lvl'];
				//不同类型的子类型数据
				$cfgList['cook']		= $G['cook'];
				$cfgList['drink']		= $G['drink'];
				$cfgList['furniture']	= $G['furniture'];
				$cfgList['cook_technics']	= arr2dimension($G['cook_technics']);
				$cfgList['cook_param']		= arr2dimension($G['cook_param']);
				$cfgList['drink_technics']	= arr2dimension($G['drink_technics']);
				$cfgList['drink_param']		= arr2dimension($G['drink_param']);
				$cfgList['furniture_technics']	= arr2dimension($G['furniture_technics']);
				$cfgList['furniture_param']		= arr2dimension($G['furniture_param']);
				$cfgList['making_item_title_list'] = array();
				foreach ($G['cook'] as $value) {
					$cfgList['making_item_title_list'][$value['item_id']] = $value['title'];
				}
				foreach ($G['drink'] as $value) {
					$cfgList['making_item_title_list'][$value['item_id']] = $value['title'];
				}
				foreach ($G['furniture'] as $value) {
					$cfgList['making_item_title_list'][$value['item_id']] = $value['title'];
				}
				//升级数据
				$cfgLvlUp = array();
				foreach ($G['make_building_lvlup_coin'] as $lvl=>$v) {
					$cfgLvlUp[$lvl] = array('need_coin' => $v);
				}
				foreach ($G['make_building_lvlup_exp'] as $lvl=>$v) {
					$cfgLvlUp[$lvl]['need_exp'] = $v;
				}
				$cfgList['makingHouse'] = array(
					'lvlUp' => $cfgLvlUp,
				);
				// 制造类型
				$cfgList['make_class_type_sign_list'] = $G['make_class_type_sign_list'];
				$cfgList['make_class_list']	= array();
				foreach ($G['make_class_list'] as $sign=>$v) {
					$cfgList['make_class_list'][$sign]['title'] = $v;
				}
				foreach ($G['make_class_type_list'] as $sign=>$v) {
					$cfgList['make_class_list'][$sign]['id'] = $v;
				}
				// 配方配置
				$cfgList['recipe'] = array();
				foreach ($G['recipe'] as $product_lvl=>$v) {
					$cfgList['recipe'][$product_lvl] = array();
					foreach ($v as $lvl=>$vv) {
						unset($vv['make_get_exp']);
						$vv['quality'] *= 100;
						$vv['lvlup_success'] *= 100;
						$cfgList['recipe'][$product_lvl][$lvl] = $vv;
					};
				}
				break;
				
			case 'shop':
				// 店铺扩建相关
				$cfg = C('build/shop');
				$cfgList['can_be_grabbed_guest_mood'] = $G['can_be_grabbed_guest_mood'];
				$cfgList['max_queue_space'] = $G['max_queue_space'];
				$cfgList['max_sale_space'] = $G['max_sale_space'];
				// 店铺仓库
				$cfgList['shop_depot'] = $G['shop_depot'];
				$cfgList['shop_depot_add_space_coin'] = $G['shop_depot_add_space_coin'];
				//店铺排队区空位扩建需要的金钱和人气值 
				$cfgList['shop_queue_space'] = $G['shop_queue_space'];
				$cfgList['shop_queue_space_coin'] = $G['shop_queue_space_coin'];
				//店铺排队区空位升级需要的金钱和人气值 
				$cfgList['shop_queue_space_lvl'] = $G['shop_queue_space_lvl'];
				$cfgList['shop_queue_space_lvl_coin'] = $G['shop_queue_space_lvl_coin'];
				//店铺销售区空位扩建需要的金钱和人气值 
				$cfgList['shop_sale_space'] = $G['shop_sale_space'];
				$cfgList['shop_sale_space_coin'] = $G['shop_sale_space_coin'];
				//店铺销售区空位升级需要的金钱和人气值 
				$cfgList['shop_sale_space_lvl'] = $G['shop_sale_space_lvl'];
				$cfgList['shop_sale_space_lvl_coin'] = $G['shop_sale_space_lvl_coin'];
				$cfgList['shop_service'] = array();
				foreach ($G['shop_service_list'] as $key=>$value) {
					$cfgList['shop_service'][$key] = $value['name'];
				}
				// 拉拢好友顾客需要的金钱
				$cfgList['lure_guest_need_coin'] = $G['lure_guest_need_coin'];
				//顾客最低品质配置
				C('shop/guest');
				$cfgList['guest_lvl_cfg'] = $G['guest_lvl_cfg'];
				break;
				
			case 'staff':
				C('making/staff');
				$cfgList['staff_state_val'] = $G['staff_state_val'];
				$cfgList['potential_lvl_list'] = $G['potential_lvl_list'];
				$cfgList['train_add_val'] = $G['train_add_val'];
				$cfgList['giveUpCard'] = $G['get_pk_prize_nochioce_ticket'];
				break;
				
			case 'equip':
				C('item/equip');
				$cfgList['equip_part'] = $G['equip_part'];
				$cfgList['equip_strengthen_success_rate'] = $G['equip_strengthen_success_rate'];
				$cfgList['equip_strengthen_need_coin'] = $G['equip_strengthen_need_coin'];
				$cfgList['add_success_rate_val_per_times'] = $G['add_success_rate_val_per_times'];
				$cfgList['luck_stone_require_cfg'] = $G['luck_stone_require_cfg'];
				$cfgList['equip_strengthen_add_abi'] = $G['equip_strengthen_add_abi'];
				foreach ($cfgList['equip_strengthen_add_abi'] as $k=>$v) {
					$cfgList['equip_strengthen_add_abi'][$k] = intval($v * 100);
				}
				// 宝石 
				$cfgList['gem_cfg'] = C('item/gem');
				$cfgList['gem_lvl_val'] = $G['gem_lvl_val'];
				$cfgList['gem_mix_success_rate'] = $G['gem_mix_success_rate'];
				$cfgList['gem_mix_need_coin'] = $G['gem_mix_need_coin'];
				$cfgList['gem_inlay_need_coin'] = $G['gem_inlay_need_coin'];
				break;
				
			case 'town':
				C('build/market');
				$cfgList['market_max_ex_coupon'] = $G['market_max_ex_coupon'];
				$cfgList['market_list_page_size'] = $G['market_list_page_size'];
				$cfgList['market_max_sale_group'] = $G['market_max_sale_group'];
				$cfgList['market_max_addon_shelf'] = $G['market_max_addon_shelf'];
				$cfgList['market_add_shelf_need_ticket'] = $G['market_add_shelf_need_ticket'];
				$cfgList['market_sale_price_max'] = $G['market_sale_price_max'];
				$cfgList['black_market_sale_rate'] = $G['black_market_sale_rate'];
				$cfgList['market_sale_exchange_coupon'] = $G['market_sale_exchange_coupon'];
				$cfgList['market_buy_exchange_coupon'] = $G['market_buy_exchange_coupon'];
				$cfgList['undersell_price_param'] = $G['undersell_price_param'];
				break;
				
			case '':
				
				break;
		}
		
		return array(
			'val'	=> true,
			'data'	=> $cfgList
		);
	}
	
	/**
	 * 开始start游戏数据
	 */
	public static function start()
	{
		global $G, $DB, $today;
		
		$uid = isset($_SESSION['uid']) ? $_SESSION['uid'] : 0;
		
		$updateData = 0;
		$today = strtotime('today');
		$now = time();
		
		// 加载配置
		self::$data = C('game');
		// 产品比赛
		C('build/making');
		if (!isset(self::$data['today_pk_product_class'])) {
			self::$data['pk_product_time']	= $today;
			self::$data['today_pk_product_class']	= 101;
			$itemCfg = $G['cook'][array_rand($G['cook'])];
			self::$data['today_pk_product_item_id']	= $itemCfg['item_id'];
			$updateData = 1;
		} else if (($today!=self::$data['pk_product_time'])) {
			if (self::$data['today_pk_product_class'] == max($G['make_class_type_list'])) {
				$class = min($G['make_class_type_list']);
			} else {
				foreach ($G['make_class_type_list'] as $class) {
					if ($class > self::$data['today_pk_product_class']) {
						break;
					}
				}
			}
			self::$data['pk_product_time']	= $today;
			self::$data['today_pk_product_class']	= $class;
			$classSign = $G['make_class_type_sign_list'][$class];
			$itemCfg = $G[$classSign][array_rand($G[$classSign])];
			self::$data['today_pk_product_item_id']	= $itemCfg['item_id'];
			$updateData = 1;
		}
		// 处理产品比赛奖励情况
		if ($uid && (empty(self::$data['pk_product_result']) || self::$data['pk_product_result'] < $today)) {
			self::$data['pk_product_result'] = $today;
			$updateData = 1;
			Making::calcPkResult($uid);
		}
		
		// 用户已登录
		if ($uid) {
			// 处理货架上已过期商品
			Market::handleEndSale($uid);
			
			// 获取用户信息
			$userInfo = User::read($uid);
			// 更新能量 点
			if ($userInfo['energy_update_time'] < $today) {
				C('common/interactive');
				$sql = 'UPDATE user SET energy_point='.$G['energy_default_value']
							.', energy_update_time='.$today.' WHERE uid='.$uid;
				$ret = $DB->exeSql($sql);
			}
			
			if (empty($_SESSION['online_time'])) {
				$_SESSION['online_time'] = $now;
				User::updateOnline($uid);
			} else if (($now - $_SESSION['online_time']) > $G['online_update_time']) {
				User::updateOnline($uid);
			}
		}
		
		
		// 配置文件内容为空
		if (empty(self::$data)) {
			self::$data['year']   = 1; // 游戏年
			self::$data['season'] = 1; // 游戏季
			self::$data['date']   = 1; // 游戏日
			// 游戏进入下一日的现实时间点(实际一天=游戏10日)
			// $G['senconds_per_day'] 每个游戏日有多少秒实际时间
			self::$data['next_date_time'] = time() + $G['senconds_per_day'];
			$updateData = 1;
		} else {//重新计算游戏数据
			// 游戏要进入下一日
			if ($now >= self::$data['next_date_time']) {
				$addDay = ceil(($now-self::$data['next_date_time'])/$G['senconds_per_day']);
				self::$data['date'] += $addDay;
				self::$data['next_date_time'] += $G['senconds_per_day']*$addDay;
				self::updateGameDate(0);
				$updateData = 1;
			}
		}
		// 如果有更新,写入新数据到配置文件中
		if ($updateData) {
			UC('game', self::$data);
		}
		
//		if (!empty($_SESSION['uid'])) {
			// 更新采集建筑富足度
//			Gather::update($_SESSION['uid']);
//		}
	}
	
	/**
	 * 更新游戏日期
	 */
	public static function updateGameDate($update2file=1)
	{
		if (self::$data['date'] > 30) {
			self::$data['date'] -= 30;
			self::$data['season']++;
		}
		if (self::$data['season'] > 4) {
			self::$data['season'] -= 4;
			self::$data['year']++;
		}
		// 不止增加一个月，递归增加
		if (self::$data['date'] > 30) {
			self::updateGameDate();
		}
		if ($update2file) {
			UC('game', self::$data);
		}
		return true;
	}
}




